//
//  YFHomeTableViewController.swift
//  微博
//
//  Created by 余仿 on 16/4/6.
//  Copyright © 2016年 YF. All rights reserved.
//

import UIKit

import SVProgressHUD

private let reuseIdentifier = "statuscell"

class YFHomeTableViewController: YFGuideTableViewController {


    /// View引用ViewModel
    lazy var statusListViewModel:YFStatusListViewModel = YFStatusListViewModel()

    //上拉加载更多指示器
    private lazy var indicatorView:UIActivityIndicatorView = {
        
        let indicator = UIActivityIndicatorView()

        indicator.frame.size.height = 44

        indicator.activityIndicatorViewStyle = .Gray

        indicator.color = UIColor.orangeColor()

        return indicator
    }()


     var  refreshCon:YFRefreshContrl?

    override func viewDidLoad() {

        super.viewDidLoad()

        setupUI()

        if isLogIn{
            loadData()
        }

    }

    //MARK:引导界面
    func setupUI(){
        if !isLogIn{

            guideView?.setupUI("visitordiscover_feed_image_smallicon", desc: "青海长云暗雪山，孤城遥望玉门关。黄沙百战穿金甲，不破楼兰终不还！",isHome: true)
        }else{

            navigationItem.rightBarButtonItem = UIBarButtonItem(imageName: "navigationbar_pop", title: nil, target: self, action: "push")

            navigationItem.leftBarButtonItem = UIBarButtonItem(imageName: "navigationbar_friendsearch", title: nil, target: nil, action: nil)

            tableView.registerClass(YFStatusCell.self, forCellReuseIdentifier: reuseIdentifier)

            tableView.rowHeight = UITableViewAutomaticDimension

            tableView.estimatedRowHeight = 300

            tableView.separatorStyle = .None

            tableView.tableFooterView = indicatorView

//            refreshControl?.addTarget(self, action: "loadData", forControlEvents: .ValueChanged)
            refreshCon = YFRefreshContrl()

            refreshCon!.addTarget(self, action: "loadData", forControlEvents: .ValueChanged)

            tableView.addSubview(refreshCon!)

        }
    }

    //MARK:加载微博数据
    func loadData(){

        statusListViewModel.loadWeiBoInfo(indicatorView.isAnimating()) { (isSuccess) -> () in

            self.refreshCon?.endRefreshing()
            self.indicatorView.stopAnimating()
              //修改状态
            if !isSuccess{
                //提示错误
                SVProgressHUD.showErrorWithStatus(AppErrorTip)
                
                return
            }



            self.tableView.reloadData()
        }
    }

    //MARK:二维码的点击事件
    func push(){

        //跳转控制器
        let temp = YFTempViewController()

        temp.navigationController?.navigationBar.tintColor = UIColor.darkGrayColor()

        navigationController?.pushViewController(temp, animated: true)
    }

    //MARK:tabBarItem点击动画效果
    
}



// MARK: - 数据源方法
extension YFHomeTableViewController{

    // MARK: - Table view data source
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return statusListViewModel.statusViewModelArray.count
    }


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        /*You must register a class or nib file using the registerNib:forCellReuseIdentifier: or registerClass:forCellReuseIdentifier: method before calling this method.
        */

//                let cell = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier) as! YFStatusCell
        //使用这个方法必须先注册cell,使用上面这个方法则不必须,如果没有事前注册cell,可能会返回nil,下面这个方法不会,因而更安全
        let cell = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier, forIndexPath: indexPath) as! YFStatusCell
        
        //        cell.textLabel?.text = statusListViewModel.statusList[indexPath.row].text
        cell.statusViewModel = statusListViewModel.statusViewModelArray[indexPath.row]

        if indexPath.row == self.statusListViewModel.statusViewModelArray.count - 1 && !indicatorView.isAnimating(){

            print("come here")

            indicatorView.startAnimating()

            loadData()
        }

        
        return cell
        
    }
    
}